package com.borqsprocessmanager;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.borqsprocessmanager.util.Log;
import com.borqsprocessmanager.util.PreferencesUtil;

public class IsRootThread extends Thread {
    private final String TAG = "RootThread";
    private final String SHAR_KEY = "Key_isRoot";

    private Handler mHandler;

    public IsRootThread(Handler handler) {
        this.mHandler = handler;
    }

    @Override
    public void run() {
        boolean isRoot = isRooted();
        // boolean isNotEng = isNotEng();
        boolean isNotEng = true;
        Log.v(TAG, "run__isRoot===" + isRoot);
        Bundle bundle = new Bundle();
        bundle.putBoolean(MainActivity.BUNDLE_KEY, isRoot && isNotEng);
        Message m = new Message();
        m.setData(bundle);
        mHandler.sendMessageDelayed(m, 0);
        Log.v(TAG, "Thread_complete");
    }

    /**
     * 先从shar中读取，如果shar是false再su判断.
     * 
     * @return
     */
    private boolean isRooted() {
        PreferencesUtil prefer = PreferencesUtil.getInstance(App.mContext);
        boolean isRoot = prefer.getBoolean(SHAR_KEY);
        Log.v(TAG, "isRoot===" + isRoot);
        if (!isRoot) {
            Log.v(TAG, "!isRoot");
            isRoot = ProcessSettingManager.isRooted();
            prefer.putBoolean(SHAR_KEY, isRoot);
        }
        return isRoot;
    }

    private boolean isNotEng() {
        Log.v(TAG, Build.TYPE + "===== Build.TYPE");
        if ("eng".equals(Build.TYPE) || "userdebug".equals(Build.TYPE)) {
            android.util.Log.v(TAG, "isEng===eng");
            return false;
        }
        return true;
    }
}
